/*验证码*/
function changeImg(){
	var code=document.getElementById("code");
  	code.src="servlet/ServletCode?"+new Date();
}

/*注册ajax实时验证*/
function register(type){
	var username = $("#username").val();
	var email = $("#email").val();
	var mobile = $("#mobile").val();
	if (type == 1) {
		if (username == '') {
			$("#span1").html("用户名不能为空!").css("color", "red");
			return;
		}
		if (!(/^\w{5,18}$/.test(username))) {
			$("#span1").html("用户名必须是5~18位!").css("color", "red");
			return;
		} else {
			ajaxCheck(type);
		}
	}

	// 邮箱
	if (type == 2) {
		if (email == '' || !(/^(\w-*\.*)+@(\w-?)+(\.\w{2,})+$/.test(email))) {
			$("#span2").html("邮箱不能为空,且必须符合邮箱格式!").css("color", "red");
			return;
		} else {
			ajaxCheck(type);
		}
	}
	
	/*//手机
	if(type == 6){
		 if(mobile == "" ){
			 $("#span6").html("手机号不能为空,且必须符合手机号格式!").css("color","red");
			 return false;
		 }else{
			 ajaxCheck(type);
		 }
	}*/
}

function ajaxCheck(type){debugger
	var username = $("#username").val();
	var password = '';
	var email = $("#email").val();
	if(username != '' && type == 3){
		password = $.md5($('#password').val() + username);
	}
	$.ajax({
		type:'POST',
		url:'checkRegister?type='+type,
		data:{username:username,password:password,email:email},//将表单序列化字符串
		dataType:'json',
		success:function(data){
			if(data && data.success){
				if(data.code == 1){//帐号可以使用
					$("#span1").html(data.message).css("color","green");
				}else if(data.code == 2){//邮箱可以使用
					$("#span2").html(data.message).css("color","green");
				}else if(data.code == 0){
					showAlertDiv(data.message);
				}
			}else{
				if(data.code == -1){//帐号已被注册
					$("#span1").html(data.message).css("color","red");
				}else if(data.code == -2){//邮箱已存在
					$("#span2").html(data.message).css("color","red");
				}else if(data.code == -3){
					alert(data.message);
				}
			}}
	});
}
	
/**
 * 注册直接提交验证
 * @returns {Boolean}
 */
function registerUser(){
	var username = $("#username").val();
	var email = $("#email").val();
	var mobile = $("#mobile").val();
	if(username == "" || !(/^\w{5,18}$/.test(username))){
	  	$("#span1").html("用户名不能为空必须是5~18位!").css("color","red");
	  	return false;
	  }else{
		  $("#span1").html("");
	  }
	 if($("#password").val() == "" ||!(/^\w{6,15}$/.test($("#password").val()))){
		$("#span3").html("密码不能为空必须是6~15位!").css("color","red");
	  	return false;
	  	}else{
	  		 $("#span3").html("");
	  	}
	
	 if($("#password").val() !=$("#confirmPassword").val() ){
		 $("#span4").html("两次输入的密码不一致!").css("color","red");
		 return false;
	 }else{
  		 $("#span4").html("");
	  	}
	 
	 if(email =='' || !(/^(\w-*\.*)+@(\w-?)+(\.\w{2,})+$/.test(email))){
		 $("#span2").html("邮箱不能为空,且必须符合邮箱格式!").css("color","red");
		 return false;
	 }else{
		  $("#span2").html("");
	  }
	 
	 if($('input[type="checkbox"]').is(':checked')){
		 ajaxCheck(3);
	 }else{
		 $("#span5").html("请仔细阅读完协议后,选择同意再进行注册!");
	 }
	 
}

/**
 * 登陆检查
 * type=1首页登陆
 * type=2 弹框登录
 * @param type
 * @returns {Boolean}
 */
function checkUser(type){debugger
	var username = $("#username").val();
	if (username == "") {
		$("#span1").html("用户名不能为空!").css("color", "red");
		return false;
	} else {
		$("#span1").html("");
	}
	if ($("#password").val() == "") {
		$("#span2").html("密码不能为空!").css("color", "red");
		return false;
	} else {
		$("#span2").html("");
	}
	loginCheck(type);
}

/**
 * 登录
 */
function loginCheck(type){debugger
	$("#login-button").val("登陆中,请稍等...");
	var username = $('#username').val();
	var password = $.md5($('#password').val() + username);
	$.ajax({
		type : 'POST',
		url : 'checkLogin',
		data : {
			"username" : username,
			"password" : password
		},
		dataType : 'json',
		success : function(data) {
			if (data && data.success) {
				if (data.code == 1) {// 登陆成功
					if (type == 1) {
						window.location.href = 'index';
					} else {
						$('#no-login').html("欢迎你," + data.object.chName).css(
								"color", "blue");
						$('#lj-login').html("").html(
								"<a href=\"logout\" >退出</a>");
						$('#free-login').html("").html("个人中心").attr("href",
								"user/userIndex").attr("target", "_black")
								.attr("title", "个人中心");
						hideDiv();
					}
				}
			} else {
				if (data.code == -3) {
					$("#span1").html(data.message);
					$("#span2").html("");
				} else if (data.code == -2) {
					$("#span1").html("");
					$("#span2").html(data.message);
				} else if (data.code == 0 || data.code == -1
						|| data.code == 2) {// 0被锁定，-1未认证,2未激活
					$("#span1").html(data.message);
				}
				$("#login-button").val("登陆");
			}
		},
	});
}

/**
 * 接受协议注册按钮才可点击
 */
function agreed(){
	if($('#agreed').is(':checked')){
		alert(1);
	}
}



